package huxibianjie.com.lbscandroid.ui.new_fragment;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;

import java.util.List;

import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.base.BaseFragment;
import huxibianjie.com.lbscandroid.manager.UserManager;
import huxibianjie.com.lbscandroid.model.AccountDetailsModel;
import huxibianjie.com.lbscandroid.model.UserModel;
import huxibianjie.com.lbscandroid.task.Callback;
import huxibianjie.com.lbscandroid.task.Task;
import huxibianjie.com.lbscandroid.ui.Service;
import huxibianjie.com.lbscandroid.ui.new_fragment.adapter.AccountDetailsAdapter;

/**
 * 账户明细
 */
@SuppressLint("ValidFragment")
public class AccountDetailsFragment extends BaseFragment {
    private RecyclerView recyclerView;
    private AccountDetailsAdapter adapter;
    private SwipeToLoadLayout swipeLayout;
    private int pageSize = 1;
    private int pageNumber = 10;
    private String type;

    public static AccountDetailsFragment getInstance(String type) {
        AccountDetailsFragment sf = new AccountDetailsFragment();
        sf.type = type;
        return sf;
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_account_details;
    }

    @Override
    protected void initView(@NonNull View view) {
        swipeLayout = view.findViewById(R.id.swipeLayout);
        recyclerView = view.findViewById(R.id.swipe_target);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemAnimator(null);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter = new AccountDetailsAdapter(getActivity(), type));
    }

    @Override
    protected void initListener(@NonNull View view) {
        swipeLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageSize = 1;
                requestData();
            }
        });
        swipeLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                pageSize++;
                requestData();
            }
        });
    }

    @Override
    protected void initData(@NonNull View view) {
        pageSize = 1;
        showLoading();
        requestData();
    }

    private void requestData() {
        if (!UserManager.getInstance().isLogin()) return;
        UserModel userModel = UserManager.getInstance().getUserModel();
        Task.create(Service.class).getCurrencyRecordList(String.valueOf(userModel.getId()), type, pageSize, pageNumber).enqueue(new Callback<List<AccountDetailsModel>>() {
            @Override
            public void onSuccess(@Nullable List<AccountDetailsModel> accountDetailsModels) {
                cancelLoading();
                if (accountDetailsModels == null)
                    return;
                if (pageSize == 1) {
                    swipeLayout.setRefreshing(false);
                    if (accountDetailsModels.size() == 0) {
                        adapter.clearAll();
                        swipeLayout.setLoadMoreEnabled(false);
                        return;
                    }
                    swipeLayout.setLoadMoreEnabled(!(accountDetailsModels.size() < 10));
                    adapter.replaceAll(accountDetailsModels);
                } else {
                    swipeLayout.setLoadingMore(false);
                    if (accountDetailsModels.size() == 0) {
                        swipeLayout.setLoadMoreEnabled(false);
                        showToast("没有更多了");
                    } else {
                        adapter.addAll(accountDetailsModels);
                    }
                }
            }

            @Override
            public void onFailure(@NonNull String error) {
                if (pageSize == 1) {
                    swipeLayout.setRefreshing(false);
                    swipeLayout.setLoadMoreEnabled(false);
                } else {
                    //数据获取失败 还原Page
                    pageSize--;
                    swipeLayout.setLoadingMore(false);
                }
                if (adapter.getItemCount() == 0) {
                }
                showToast(error);
                cancelLoading();
            }
        });


    }

}